package com.huawei.hms.api;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import com.huawei.hianalytics.hms.HiAnalytics;
import com.huawei.hms.api.Api.ApiOptions;
import com.huawei.hms.api.Api.ApiOptions.HasOptions;
import com.huawei.hms.api.Api.ApiOptions.NotRequiredOptions;
import com.huawei.hms.framework.network.grs.GrsBaseInfo;
import com.huawei.hms.framework.network.grs.GrsClient;
import com.huawei.hms.framework.network.grs.IQueryUrlCallBack;
import com.huawei.hms.framework.network.restclient.dnkeeper.DNKeeperConfig;
import com.huawei.hms.support.api.client.AidlApiClient;
import com.huawei.hms.support.api.client.SubAppInfo;
import com.huawei.hms.support.api.entity.auth.PermissionInfo;
import com.huawei.hms.support.api.entity.auth.Scope;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant.KeyAndValue;
import com.huawei.hms.support.hianalytics.HiAnalyticsUtil;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.Checker;
import com.huawei.hms.utils.ResourceLoaderUtil;
import com.huawei.hms.utils.Util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class HuaweiApiClient implements AidlApiClient {

    public interface ConnectionCallbacks {
        public static final int CAUSE_API_CLIENT_EXPIRED = 3;
        public static final int CAUSE_NETWORK_LOST = 2;
        public static final int CAUSE_SERVICE_DISCONNECTED = 1;

        void onConnected();

        void onConnectionSuspended(int i);
    }

    public interface OnConnectionFailedListener {
        void onConnectionFailed(ConnectionResult connectionResult);
    }

    public static final class Builder {
        private final Context a;
        private final List<Scope> b = new ArrayList();
        private final List<PermissionInfo> c = new ArrayList();
        private final Map<Api<?>, ApiOptions> d = new HashMap();
        private OnConnectionFailedListener e;
        private ConnectionCallbacks f;

        public Builder(Context context) throws NullPointerException {
            Checker.checkNonNull(context, "context must not be null.");
            this.a = context.getApplicationContext();
            ResourceLoaderUtil.setmContext(this.a);
            a(context);
        }

        private void a(Context context) {
            boolean initFlag = HiAnalytics.getInitFlag();
            HMSLog.i("HMSBI", "Builder->biInitFlag :" + initFlag);
            if (!initFlag && Util.isChinaROM()) {
                initFlag = Util.getBiSetting(this.a);
                HMSLog.i("HMSBI", "Builder->biSetting :" + initFlag);
                if (!initFlag) {
                    GrsBaseInfo grsBaseInfo = new GrsBaseInfo();
                    grsBaseInfo.setAppName("opensdkservice");
                    grsBaseInfo.setIssueCountry("CN");
                    new GrsClient(context, grsBaseInfo).ayncGetGrsUrl("com.huawei.cloud.hianalytics.issue", DNKeeperConfig.SERVICE_KEY, new IQueryUrlCallBack(this) {
                        final /* synthetic */ Builder a;

                        {
                            this.a = r1;
                        }

                        public void onCallBackSuccess(String str) {
                            if (!TextUtils.isEmpty(str)) {
                                new com.huawei.hianalytics.hms.HiAnalyticsConf.Builder(this.a.a).setEnableImei(false).setEnableUDID(false).setEnableSN(false).setCollectURL(0, str).create();
                            }
                        }

                        public void onCallBackFail(int i) {
                            HMSLog.e("HMSSDK_HuaweiApiClient", "get grs failed, the errorcode is " + i);
                        }
                    });
                }
            }
        }

        public HuaweiApiClient build() {
            addApi(new Api("Core.API"));
            HuaweiApiClient huaweiApiClientImpl = new HuaweiApiClientImpl(this.a);
            huaweiApiClientImpl.setScopes(this.b);
            huaweiApiClientImpl.setPermissionInfos(this.c);
            huaweiApiClientImpl.setApiMap(this.d);
            huaweiApiClientImpl.setConnectionCallbacks(this.f);
            huaweiApiClientImpl.setConnectionFailedListener(this.e);
            return huaweiApiClientImpl;
        }

        public Builder addConnectionCallbacks(ConnectionCallbacks connectionCallbacks) {
            Checker.checkNonNull(connectionCallbacks, "listener must not be null.");
            this.f = connectionCallbacks;
            return this;
        }

        public Builder addOnConnectionFailedListener(OnConnectionFailedListener onConnectionFailedListener) {
            Checker.checkNonNull(onConnectionFailedListener, "listener must not be null.");
            this.e = onConnectionFailedListener;
            return this;
        }

        public Builder addScope(Scope scope) {
            Checker.checkNonNull(scope, "scope must not be null.");
            this.b.add(scope);
            return this;
        }

        public Builder addApi(Api<? extends NotRequiredOptions> api) {
            this.d.put(api, null);
            if (HuaweiApiAvailability.HMS_API_NAME_GAME.equals(api.getApiName())) {
                HiAnalyticsUtil.getInstance().onEvent(this.a.getApplicationContext(), KeyAndValue.GAME_INIT_KEY, HiAnalyticsConstant.REPORT_VAL_SEPARATOR + System.currentTimeMillis());
            }
            return this;
        }

        public <O extends HasOptions> Builder addApi(Api<O> api, O o) {
            Checker.checkNonNull(api, "Api must not be null");
            Checker.checkNonNull(o, "Null options are not permitted for this Api");
            this.d.put(api, o);
            if (api.getOptions() != null) {
                this.b.addAll(api.getOptions().getScopeList(o));
                this.c.addAll(api.getOptions().getPermissionInfoList(o));
            }
            return this;
        }
    }

    public abstract void checkUpdate(Activity activity, CheckUpdatelistener checkUpdatelistener);

    public abstract void connect(Activity activity);

    public abstract void connectForeground();

    public abstract void disconnect();

    public abstract Map<Api<?>, ApiOptions> getApiMap();

    public abstract Activity getTopActivity();

    public abstract boolean isConnected();

    public abstract boolean isConnecting();

    public abstract void onPause(Activity activity);

    public abstract void onResume(Activity activity);

    public abstract void setConnectionCallbacks(ConnectionCallbacks connectionCallbacks);

    public abstract void setConnectionFailedListener(OnConnectionFailedListener onConnectionFailedListener);

    public abstract boolean setSubAppInfo(SubAppInfo subAppInfo);
}
